import { _decorator, Component, Node, view } from "cc";
import { SetBasicInfo } from "../Utils/SetBasicInfo";
const { ccclass, property } = _decorator;

@ccclass("背景")
export class Bg extends Component {
  // 当前屏幕宽高
  pageWidth: number = 0;
  pageHeight: number = 0;

  // 背景1
  @property(Node)
  bg01: Node = null;
  // 背景2
  @property(Node)
  bg02: Node = null;

  // 移动速度 (背景)
  @property
  speed: number = 100;

  protected start(): void {
    this.getBgHeight();
  }

  update(deltaTime: number) {
    // 移动
    let position1 = this.bg01.position;
    SetBasicInfo.setPosition(this.bg01, {
      y: position1.y - this.speed * deltaTime,
    });
    let position2 = this.bg02.position;
    SetBasicInfo.setPosition(this.bg02, {
      y: position2.y - this.speed * deltaTime,
    });

    // 重置
    let p1 = this.bg01.position;
    let p2 = this.bg02.position;
    if (this.bg01.position.y < -this.pageHeight)
      SetBasicInfo.setPosition(this.bg01, {
        y: p2.y + this.pageHeight,
      });
    if (this.bg02.position.y < -this.pageHeight)
      SetBasicInfo.setPosition(this.bg02, {
        y: p1.y + this.pageHeight,
      });
  }

  getBgHeight() {
    const { width, height } = view.getVisibleSize();
    this.pageWidth = width;
    this.pageHeight = height;
  }
}
